package cn.com.bluemoon.daps.system.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.dto.SysPageDto;
import cn.com.bluemoon.daps.system.dto.SysUserAddDto;
import cn.com.bluemoon.daps.system.dto.SysUserUpdateDto;
import cn.com.bluemoon.daps.system.entity.DapSysUser;
import cn.com.bluemoon.daps.system.entity.DapSysUserRole;
import cn.com.bluemoon.daps.system.service.DapSysUserRoleService;
import cn.com.bluemoon.daps.system.service.DapSysUserService;
import cn.com.bluemoon.daps.system.vo.DapSysUserVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-12-19
 */
@Api(tags = {"系统用户管理"})
@RestController
@Slf4j
@RequestMapping("/dapSysUser")
public class DapSysUserController {

    @Autowired
    private DapSysUserService sysUserService;

    @Autowired
    private DapSysUserRoleService dapSysUserRoleService;

    @ApiOperation("用户列表")
    @PostMapping(value = "/user/page")
    @BmPermission(value = "dap:system:user:page",ignore = true)
    public ResultBean<IPage<DapSysUserVo>> page(@RequestBody SysPageDto pageDto){
        IPage<DapSysUserVo> userPage = sysUserService.userPage(pageDto);
        return ResultBean.ok(userPage);
    }

    @ApiOperation("根据ID获取用户")
    @GetMapping(value = "/user/getUserById")
    @BmPermission(value = "dap:system:user:getUserById",ignore = true)
    public ResultBean<DapSysUserVo> getUserById(@RequestParam("id") Long id){
        DapSysUserVo userVo = new DapSysUserVo();
        DapSysUser sysUser = sysUserService.getById(id);
        if(Objects.isNull(sysUser)){
            return ResultBean.error(String.format("该用户已不存在"));
        }
        List<DapSysUserRole> list = dapSysUserRoleService.lambdaQuery().eq(DapSysUserRole::getUserId, id).list();
        if (CollectionUtils.isNotEmpty(list)) {
            userVo.setRoleId(list.stream().map(x -> x.getRoleId()).collect(Collectors.toList()));
        }
        BeanUtils.copyProperties(sysUser,userVo);
        return ResultBean.ok(userVo);
    }

    @ApiOperation("新增用户")
    @PostMapping(value = "/user/add")
    @BmPermission(value = "dap:system:user:add",ignore = true)
    public ResultBean<Boolean> add(@RequestBody SysUserAddDto dto){
        return ResultBean.ok(sysUserService.addUser(dto));
    }

    @ApiOperation("修改用户")
    @PostMapping(value = "/user/update")
    @BmPermission(value = "dap:system:user:update",ignore = true)
    public ResultBean<Boolean> update(@RequestBody SysUserUpdateDto dto){
        return ResultBean.ok(sysUserService.updateUser(dto));
    }

    @ApiOperation("删除用户")
    @DeleteMapping(value = "/user/delete")
    @BmPermission(value = "dap:system:user:delete",ignore = true)
    public ResultBean<Boolean> delete(@RequestParam("id") Long id){
        return ResultBean.ok(sysUserService.deleteUser(id));
    }

    @ApiOperation("根据ID重置用户密码")
    @GetMapping(value = "/user/resetPwd")
    @BmPermission(value = "dap:system:user:resetPwd",ignore = true)
    public ResultBean<Boolean> resetPwd(@RequestParam("id") Long id,@RequestParam("password") String password){
        return ResultBean.ok(sysUserService.resetPwd(id,password));
    }

    @ApiOperation("修改用户密码")
    @GetMapping(value = "/user/changePwd")
    @BmPermission(value = "dap:system:user:changePwd",ignore = true)
    public ResultBean<Boolean> changePwd(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){
        return ResultBean.ok(sysUserService.changePwd(oldPassword,newPassword));
    }
}
